﻿/// <reference path="JSintellisense/jquery-1.3.2-vsdoc2.js" />

///提交订单
function checkorder() {
    var tbname = $("#tbname").html();
    if (tbname == "") {
        alert("请输入您的称呼.");
        //$("#nofoodnotice").html("请输入您的称呼.");
        //$("#nofoodnotice").popup("open");
        return false;
    }


    var tbtel = $("#tbtel").html();
    if (tbtel == undefined) {//说明没有地址 2015-12-09
        alert("亲，新增地址没有填呦，没法配送噢！")
        return false;
    }
    if (tbtel == "") {
        alert("请输入您的电话.")
        return false;
    }
    var myreg = /^1\d{10}$/;
    if (!myreg.test(tbtel)) {
        alert("手机(电话)号码格式错误.")
        return false;
    }


    var ddltime = $("#ddltime").val();
    if (ddltime == "当前不配送") {
        alert("当前不配送.")
        //$("#nofoodnotice").html("当前不配送.");
        //$("#nofoodnotice").popup("open");
        return false;
    }

    var tbaddress = $("#tbaddress").html();
    if (tbaddress == "") {
        alert("请输入您的详细地址.")
        //$("#nofoodnotice").html("请输入您的详细地址.");
        //$("#nofoodnotice").popup("open");
        return false;
    }
    var paymodel = $("input[name=ddlpaymode]:checked").val();
    if (paymodel == "3") {

        var tbpaypwd = $("#tbpaypwd").val();
        if (tbpaypwd == "") {
            alert("请输入支付密码.")
            return false;
        }
    }


    //保存用户信息
    orderdetail = {
        username: tbname,
        phone: tbtel,
        address: tbaddress
    }
    cart.saveUserInfo(orderdetail);
    $("#myordermsgk").show();
    $("#noaddresstip").show();
    return true;
}

$(function () {
    //获取所有购物车中的商品
    var ShoppingCart = utils.getParam("ShoppingCart");
    if (ShoppingCart != null && ShoppingCart != "") {
        $("#hfproductjson").val(ShoppingCart);
        var productlist = cart.getproductlist();
        $(".cart-num").html(orderdetail.totalNumber + "");


        var allprice = parseFloat(orderdetail.totalAmount);
        //满多少免费送费
        var freemoney = parseFloat($("#hffreemoney").val());//满多少免费送费
        var sendfree = parseFloat($("#hfsendfree").val());
        if (orderdetail.totalAmount >= freemoney && freemoney > 0) {
            sendfree = 0;
        }
        $(".money_num").html("￥" + (allprice + sendfree).toFixed(1));





    }
    else {
        window.location = "ShowTogo.aspx?id=" + request("id");
    }

    $("#tbmydate").click(function () {
        $(".ui-input-datebox a").click();
    })

    //读取用户信息
    cart.getUserInfo();
    var tbname = $("#tbname").val() + "";
    if (orderdetail.username != "" && tbname == "") {
        $("#tbname").val(orderdetail.username);
        $("#tbtel").val(orderdetail.phone);
        $("#tbaddress").val(orderdetail.address);
    }

    $(".order-title").click(function () {
        $(this).find("label")
            .removeClass("no-checked")
            .addClass("checked").parent().parent().siblings()
            .find("label")
            .removeClass("checked")
            .addClass("no-checked");
        //当点击时将intput设置为选中并且将其他input设置为未选中
        $(this).find("input[name=ddlpaymode]").attr("checked", true).parent().parent().siblings().find("input[name=ddlpaymode]").attr("checked", false);

        if ($(this).find("input[name=ddlpaymode]").val() == "3") {
            $(".order-psw").show();
        }
        else {
            $(".order-psw").hide();
        }
    })

    $("#btcartbox").click(function () {
        var url = "cartdetail.aspx?id=" + request("id");
        window.location = url;
        return false;
    });


    $("#btsubmit").click(function () {

        var flag = checkorder();
        if (false == flag) {
            return false;
        }

       // WeUI.showLoading();
        var options = {
            url: 'orderdetail.aspx?method=addorder&id=' + request("id"),
            type: 'post',
            dataType: 'text',
            data: $("#form1").serialize(),
            success: function (data) {


                var json = eval("(" + data + ")");
                if (json.state == "1") {

                    //如果是微信支付，直接开始支付

                    // alert(json.data.paydata.timeStamp);

                    localStorage.setItem("ShoppingCart", "");
                    window.location = json.data.url;//支付去支付
                    //删除购物车
                }
                else {
                    $("#btsubmit").show();
                    WeUI.hideLoadingslowly();
                    swal({
                        title: "温馨提示",
                        text: json.msg,
                        type: "warning"
                    });

                    $("#myordermsgk").hide();
                    $("#noaddresstip").hide();
                }

            }
        };
        $.ajax(options);
        return false;
    });



})

function addremark(id) {
    window.location.href = "remarkdetail.aspx?id=" + id;
}
